//package com.yc.commontlib.base
//
//import android.content.Context
//import android.os.Handler
//import android.os.Looper
//import android.view.LayoutInflater
//import android.view.View
//import androidx.annotation.MainThread
//import androidx.appcompat.app.AppCompatActivity
//import androidx.lifecycle.*
//import androidx.viewbinding.ViewBinding
//import kotlin.properties.ReadOnlyProperty
//import kotlin.reflect.KFunction1
//import kotlin.reflect.KProperty
//
///**
// * Creator: yc
// * Date: 2021/6/9 16:47
// * UseDes:
// */
//abstract class BaseActivity : AppCompatActivity() {
//    //   val  mViewBinding:ViewBinding by
////   fun <A:C>
////    fun <V : ViewBinding> viewBinding(clazz: Class<V>): ReadOnlyProperty<> {
////
////    }
//    //为什么可以使用 V::class.java，不是泛型擦除了吗？
//    // 利用了 Kotlin 内敛函数 + 实化类型参数，编译后函数体整体被复制到调用处，V::class.java 其实是 FragmentTestBinding::class.java
//    public inline fun <reified V : ViewBinding> viewBindingV1() = viewBindingV1(V::class.java)
//
//    public inline fun <reified T : ViewBinding> viewBindingV1(clazz: Class<T>): FragmentViewBindingPropertyV1<AppCompatActivity, T> {
//        val bindMethod = clazz.getMethod("inflate", View::class.java)
//        return FragmentViewBindingPropertyV1 {
//            bindMethod(null, LayoutInflater.from(it as Context)) as T
//        }
//    }
//
//     fun <A : AppCompatActivity, V : ViewBinding> viewBindingV2(
//         viewBinder: (LayoutInflater) -> V,
//         viewProvider: (A) -> LayoutInflater = {
//            LayoutInflater.from(it as Context)
//        }
//    ) = FragmentViewBindingPropertyV2 { activity: A ->
//        viewBinder(viewProvider(activity)).apply {
//            activity.lifecycle.addObserver(object : DefaultLifecycleObserver {
//                override fun onCreate(owner: LifecycleOwner) {
//                    super.onCreate(owner)
//                    setContentView(this@apply.root)
//                }
//            })
//        }
//    }
//
//    class FragmentViewBindingPropertyV2<in F : AppCompatActivity, out V : ViewBinding>(private val viewBinder: (F) -> V) : ReadOnlyProperty<F, V> {
//        private var viewBinding: V? = null
//
//        @MainThread
//        override fun getValue(thisRef: F, property: KProperty<*>): V {
//            // 已经绑定，直接返回
//            viewBinding?.let { return it }
//            // Use viewLifecycleOwner.lifecycle other than lifecycle
//            val lifecycle = thisRef.lifecycle
//            val viewBinding = viewBinder(thisRef)
//            if (lifecycle.currentState == Lifecycle.State.DESTROYED) {
//
//            } else {
//                lifecycle.addObserver(ClearOnDestroyLifecycleObserver())
//                this.viewBinding = viewBinding
//            }
//            return viewBinding
//        }
//
//        @MainThread
//        fun clear() {
//            viewBinding = null
//        }
//
//        private inner class ClearOnDestroyLifecycleObserver : LifecycleObserver {
//            private val mainHandler = Handler(Looper.getMainLooper())
//
//            @MainThread
//            @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
//            fun onDestroy(owner: LifecycleOwner) {
//                owner.lifecycle.removeObserver(this)
//                mainHandler.post { clear() }
//            }
//        }
//    }
//
//    class FragmentViewBindingPropertyV1<in F : AppCompatActivity, out V : ViewBinding>(private val viewBinder: (F) -> V) : ReadOnlyProperty<F, V> {
//        private var viewBinding: V? = null
//
//        @MainThread
//        override fun getValue(thisRef: F, property: KProperty<*>): V {
//            // 已经绑定，直接返回
//            viewBinding?.let { return it }
//            // Use viewLifecycleOwner.lifecycle other than lifecycle
//            val lifecycle = thisRef.lifecycle
//            val viewBinding = viewBinder(thisRef)
//            if (lifecycle.currentState == Lifecycle.State.DESTROYED) {
//
//            } else {
//                lifecycle.addObserver(ClearOnDestroyLifecycleObserver())
//                this.viewBinding = viewBinding
//            }
//            return viewBinding
//        }
//
//        @MainThread
//        fun clear() {
//            viewBinding = null
//        }
//
//        private inner class ClearOnDestroyLifecycleObserver : LifecycleObserver {
//            private val mainHandler = Handler(Looper.getMainLooper())
//
//            @MainThread
//            @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
//            fun onDestroy(owner: LifecycleOwner) {
//                owner.lifecycle.removeObserver(this)
//                mainHandler.post { clear() }
//            }
//        }
//    }
//}